package com.wuyou.simple;

import java.util.Arrays;

/**
 * 1652. 拆炸弹
 */
public class Decrypt {


    public static void main(String[] args) {
        System.out.println(6 % 4);
        Decrypt decrypt = new Decrypt();
        int[] code = {5, 7, 1, 4};
        int k = 3;
        System.out.println(decrypt.solution_1(code, k));

    }

    public int[] solution_1(int[] code, int k) {
        int[] res = new int[code.length];

        if (k > 0) {
            for (int i = 0; i < res.length; i++) {
                res[i] = sumN(code, k, i);
            }
        } else if (k < 0) {
            for (int i = 0; i < res.length; i++) {
                res[i] = sumReverseN(code, k, i);
            }
        } else {
            Arrays.fill(res, 0);
        }
        return res;
    }

    public int sumReverseN(int[] code, int k, int j) {
        int res = 0;
        int count = 0;

        int index = (j - 1) % k;
        while (count < k) {
            res += code[index];
            count++;
        }
        return res;
    }

    public int sumN(int[] code, int k, int j) {
        int res = 0;
        int count = 1;

        while (count <= k) {
            int index = (j + count) % k;
            res += code[index];
            count++;
        }
        return res;
    }
}
